﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Fireball.Extensions
{
    public static class ArrayExtensions
    {
        /// <summary>
        /// Return the md5 Hash for the specified array of bytes
        /// </summary>
        /// <param name="bts">The byte array where caltulate the md5 hash</param>
        /// <returns>The resulting md5 hash as string</returns>
        public static string GetMd5(this byte[] bts)
        {
            if (bts == null || bts.Length == 0)
                return null;

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            UTF8Encoding enc = new UTF8Encoding();
            byte[] hash = md5.ComputeHash(bts);
            StringBuilder buff = new StringBuilder();

            for (int i = 0; i < hash.Length; i++)
            {
                buff.Append(String.Format("{0:x2}", hash[i]));
            }

            return buff.ToString();
        }
    }
}
